<?php

namespace app\common\repositories\activity\lottery;


use app\common\dao\activity\lottery\LuckLotteryRecordDao;
use app\common\repositories\BaseRepository;
use app\common\repositories\store\coupon\StoreCouponRepository;
use app\common\repositories\store\coupon\StoreCouponUserRepository;
use app\common\repositories\user\UserBillRepository;
use app\common\repositories\user\UserIntegralBillRepository;
use app\common\repositories\user\UserRepository;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Log;

class LuckLotteryRecordRepository extends BaseRepository
{
    public function __construct(LuckLotteryRecordDao  $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 获取抽奖记录列表
     * @param array $where
     * @return array
     */
    public function getList(array $where,$page, $limit)
    {

        $where['not_type'] = 1;
        if (isset($where['factor']) && $where['factor']) {
            $luckServices = app()->make(LuckLotteryRepository::class);
            $where['lottery_id'] = $luckServices->query(['factor' => $where['factor'], 'status' => 1])->value('id');
            if (!$where['lottery_id']) {
                $list = [];
                $count = 0;
                return compact('list', 'count');
            }
            unset($where['factor']);
        }
        $query = $this->dao->getList($where, '*', ['lottery', 'prize', 'user'], $page, $limit);
        $count = $query->count();
        $list = $query->select()->toArray();
        if ($list) {
            $prizeType = app()->make(LuckLotteryPrizeRepository::class)->prize_type;
            foreach ($list as &$item) {
                if (isset($item['prize_info']) && $item['prize_info']) {
                    $prize = json_decode($item['prize_info'], true);
                } else {
                    $prize = $item['prize'];
                }
                if (!$item['user']) {
                    $item['user']['nickname'] = '用户已注销';
                    $item['user']['real_name'] = '用户已注销';
                    $item['user']['phone'] = '';
                    $item['user']['uid'] = 0;
                    $item['user']['delete_time'] = null;
                }
                $prize['type_name'] = $prizeType[$prize['type']] ?? '未知';
                $item['prize'] = $prize;
            }
        }

        return compact('list', 'count');
    }

    /**
     * 获取中奖记录
     * @param array $where
     * @param int $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getWinList(array $where, int $limit = 10)
    {
        //去除未中奖的
        $where = $where + ['not_type' => 1];

        $list = $this->dao->getList($where, 'id,uid,prize_id,lottery_id,receive_time,prize_info,add_time', ['user', 'prize'], 0, $limit)->select()->toArray();
        foreach ($list as &$item) {
            if (isset($item['prize_info']) && $item['prize_info']) {
                $prize = json_decode($item['prize_info'], true);
                unset($item['prize_info']);
            } else {
                $prize = $item['prize'];
            }
            $item['prize'] = [
                'id' => $prize['id'] ?? 0,
                'type' => $prize['type'] ?? 1,
                'name' => $prize['name'] ?? '',
                'image' => $prize['image'] ?? '',
                'prompt' => $item['prompt'] ?? '',
            ];
            $item['receive_time'] = $item['receive_time'] ? date('Y-m-d H:i:s', $item['receive_time']) : '';
        }
        return $list;
    }

    /**
     * 参与抽奖数据统计
     * @param int $lottery_id
     * @return int[]
     */
    public function getLotteryRecordData(int $lottery_id)
    {
        $data = ['all' => 0, 'people' => 0, 'win' => 0];
        if ($lottery_id) {
            $where = [['lottery_id', '=', $lottery_id]];
            $data['all'] = $this->dao->getCount($where);
            $data['people'] = $this->dao->getCount($where, 'uid');
            $data['win'] = $this->dao->getCount($where + [['type', '>', 1]], 'uid');
        }
        return $data;
    }

    /**
     * 写入中奖纪录
     * @param int $uid
     * @param array $prize
     * @return mixed
     */
    public function insertPrizeRecord(int $uid, array $prize, array $userInfo = [])
    {
        if (!$userInfo) {
            $userServices = app()->make(UserRepository::class);
            $userInfo = $userServices->get($uid);
        }
        if (!$userInfo) {
            throw new ValidateException('用户不存在');
        }
        if (!$prize) {
            throw new ValidateException('奖品不存在');
        }
        $data = [];
        $data['uid'] = $uid;
        $data['lottery_id'] = $prize['lottery_id'];
        $data['prize_id'] = $prize['id'];
        $data['type'] = $prize['type'];
        $data['prize_info'] = json_encode($prize);
        if (!$res = $this->dao->create($data)) {
            throw new ValidateException('写入中奖记录失败');
        }
        return $res;
    }

    /**
     * 领取奖品
     * @param int $uid
     * @param int $lottery_record_id
     * @param string $receive_info
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function receivePrize(int $uid, int $lottery_record_id, array $receive_info = [])
    {
        $userServices = app()->make(UserRepository::class);
        $userInfo = $userServices->get($uid);
        if (!$userInfo) {
            throw new ValidateException('用户不存在');
        }
        $lotteryRecord = $this->dao->get($lottery_record_id, ['prize_id', 'lottery_id', 'id', 'is_receive'], ['prize']);
        if (!$lotteryRecord || !isset($lotteryRecord['prize'])) {
            throw new ValidateException('请继续参与活动抽奖');
        }
        if ($lotteryRecord['is_receive'] == 1) {
            throw new ValidateException('已经领取成功');
        }
        $data = ['is_receive' => 1, 'receive_time' => time(), 'receive_info' => is_array($receive_info) ? json_encode($receive_info) : $receive_info];
        if (isset($lotteryRecord['prize_info']) && $lotteryRecord['prize_info']) {
            $prize = json_decode($lotteryRecord['prize_info'], true);
        } else {
            $prize = $lotteryRecord['prize'];
        }
        Db::transaction(function () use ($uid, $userInfo, $lottery_record_id, $data, $prize, $userServices, $receive_info) {
            //奖品类型1：未中奖2：积分3:余额4：红包5:优惠券6：站内商品7：等级经验8：用户等级 9：svip天数
            switch ($prize['type']) {
                case 1:
                case 7:
                case 8:
                case 9:
                    break;
                case 2:
                    $userIntegralBillRepository = app()->make(UserIntegralBillRepository::class);
                    $integral = bcadd((string)$userInfo['integral'], (string)$prize['num'], 0);
                    $userIntegralBillRepository->incBill( $uid, 'integral','lottery_add',[
                        'link_id' => $prize['id'],
                        'status' => 1,
                        'title' => '抽奖中奖赠送积分',
                        'number' => $prize['num'],
                        'mark' => '抽奖中奖赠送' . $prize['num'] . '积分',
                        'balance' => $integral
                    ]);
                    $userServices->update($uid, ['integral' => $integral]);
                    break;
                case 3:
                    $userBillRepository = app()->make(UserBillRepository::class);
                    $now_money = bcadd((string)$userInfo['now_money'], (string)$prize['num'], 2);
                    $userBillRepository->incBill( $uid, 'now_money','lottery_money_add',[
                        'link_id' => $prize['id'],
                        'status' => 1,
                        'title' => '抽奖中奖赠送余额',
                        'number' => $prize['num'],
                        'mark' => '抽奖中奖赠送' . $prize['num'] . '余额',
                        'balance' => $now_money
                    ]);
                    $userServices->update($uid, ['now_money' => $now_money]);
                    break;
                case 4:
//                    /** @var WechatUserServices $wechatServices */
//                    $wechatServices = app()->make(WechatUserServices::class);
//                    $openid = $wechatServices->getWechatOpenid($uid, 'wechat');
//                    if ($openid) {//公众号用户
//                        $type = Payment::WEB;
//                    } else {//小程序用户
//                        $openid = $wechatServices->getWechatOpenid($uid, 'routine');
//                        $type = Payment::MINI;
//                    }
//                    //app微信用户
//                    if (!$openid) {
//                        $openid = $wechatServices->getWechatOpenid($uid, 'app');
//                        $type = Payment::APP;
//                    }
//                    if ($openid) {
//                        /** @var StoreOrderCreateServices $services */
//                        $services = app()->make(StoreOrderCreateServices::class);
//                        $wechat_order_id = $services->getNewOrderId();
//                        //记录资金流水
//                        CapitalFlowJob::dispatch([['order_id' => $wechat_order_id, 'store_id' => 0, 'uid' => $uid, 'price' => $prize['num'], 'pay_type' => 'weixin', 'nickname' => $userInfo['nickname'], 'phone' => $userInfo['phone']], 'luck']);
//                        Payment::merchantPay($openid, $wechat_order_id, (string)$prize['num'], '抽奖中奖红包', $type);
//                    }
                    break;
                case 5:
                    $couponIssueService = app()->make(StoreCouponRepository::class);
                    try {
                        $couponIssueService->issueUserCoupon((int)$prize['coupon_id'],$uid);
                    } catch (\Throwable $e) {
                        Log::error('抽奖领取优惠券失败，原因：' . $e->getMessage());
                    }
                    break;
                case 6:
                    if (!$receive_info['name'] || !$receive_info['phone'] || !$receive_info['address']) {
                        throw new ValidateException('请输入收货人信息');
                    }
                    if (!isPhone($receive_info['phone'])) {
                        throw new ValidateException('请输入正确的收货人电话');
                    }
                    break;
            }
            $this->dao->update($lottery_record_id, $data);
        });
        return true;
    }

    /**
     * 发货、备注
     * @param int $lottery_record_id
     * @param array $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function setDeliver(int $lottery_record_id, array $data)
    {
        $lotteryRecord = $this->dao->get($lottery_record_id, ['deliver_info', 'type', 'id']);
        if (!$lotteryRecord) {
            throw new ValidateException('抽奖记录不存在');
        }
        $deliver_info = $lotteryRecord['deliver_info'];
        $edit = [];
        //备注
        if (!$data['deliver_name'] && !$data['deliver_number']) {
            $deliver_info['mark'] = $data['mark'];
        } else {
            if ($lotteryRecord['type'] != 6 && ($data['deliver_name'] || $data['deliver_number'])) {
                throw new ValidateException('该奖品不需要发货');
            }
            if ($lotteryRecord['type'] == 6 && (!$data['deliver_name'] || !$data['deliver_number'])) {
                throw new ValidateException('请选择快递公司或输入快递单号');
            }
            $deliver_info['deliver_name'] = $data['deliver_name'];
            $deliver_info['deliver_number'] = $data['deliver_number'];
            $edit['is_deliver'] = 1;
            $edit['deliver_time'] = time();
        }
        $edit['deliver_info'] = $deliver_info;
        if (!$this->dao->update($lottery_record_id, $edit)) {
            throw new ValidateException('处理失败');
        }
        return true;
    }

    /**
     * 获取中奖记录
     * @param int $uid
     * @return array
     */
    public function getRecord(int $uid, $where = [],$page,$limit)
    {
        if (!$where) {
            $where['uid'] = $uid;
            $where['not_type'] = 1;
        }
        $list = $this->dao->getList($where, '*', ['prize'], $page, $limit)->select()->toArray();
        foreach ($list as &$item) {
            if (isset($item['prize_info']) && $item['prize_info']) {
                $prize = $item['prize_info'] = json_decode($item['prize_info'], true);
            } else {
                $prize = $item['prize'];
            }
            $item['prize'] = $prize;
            $item['deliver_time'] = $item['deliver_time'] ? date('Y-m-d H:i:s', $item['deliver_time']) : '';
            $item['receive_time'] = $item['receive_time'] ? date('Y-m-d H:i:s', $item['receive_time']) : '';
        }
        return $list;
    }

    /**
     * 获取抽奖次数
     * @param int $uid
     * @param int $lottery_id
     * @return array
     */
    public function getLotteryNum(int $uid, int $lottery_id)
    {
        $where['uid'] = $uid;
        $where['lottery_id'] = $lottery_id;
        $now_day = strtotime(date('Y-m-d'));//今日
        $todayCount = $this->dao->getCount($where + ['add_time' => $now_day]);
        $totalCount = $this->dao->getCount($where);
        return compact('todayCount', 'totalCount');
    }



}
